.PHONY: run run_pause debug clean

ARCH = arm
# CPU = arm926ej-s
CPU = cortex-m3
# MACHINE = integratorcp
MACHINE = stm32vldiscovery
TOOLCHAIN = arm-none-eabi
# FLAGS = -g
FLAGS = -g -mthumb
OBJECTS = cpu/$(CPU)/init.o machine/$(MACHINE)/interrupt.o machine/$(MACHINE)/timer.o machine/$(MACHINE)/uart.o machine/$(MACHINE)/lcd.o entry.o

%.o: %.s
	$(TOOLCHAIN)-as -mcpu=$(CPU) $(FLAGS) $< -o $@

%.o: %.c
	$(TOOLCHAIN)-gcc -mcpu=$(CPU) $(FLAGS) -Iinclude -c $< -o $@

test.bin: test.elf
	$(TOOLCHAIN)-objcopy -O binary test.elf test.bin

test.elf: cpu/$(CPU)/link.ld $(OBJECTS)
	$(TOOLCHAIN)-ld -T cpu/$(CPU)/link.ld $(OBJECTS) -o test.elf

run:
	qemu-system-$(ARCH) -machine $(MACHINE) -device loader,file=test.bin -serial mon:stdio

run_pause:
	qemu-system-$(ARCH) -machine $(MACHINE) -device loader,file=test.bin -serial mon:stdio -S -s

debug:
	$(TOOLCHAIN)-gdb --eval-command="target remote localhost:1234" test.elf

clean:
	del /S *.o *.elf *.bin
